package org.dream.ui.chart;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import org.dream.util.ReflectUtil;

/**
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class Polyline extends Plot {

	public Color lineColor;
	public int ovalRadius;
	protected List<Double> xData = new ArrayList<Double>();
	protected List<Double> yData = new ArrayList<Double>();
	protected List<String> strData = new ArrayList<String>();
	private boolean showLine = true;

	public Polyline(Chart chart, int index) {
		super(chart, index);
		lineColor = Color.BLUE;
	}

	@Override
	public Double getMaxY() {
		Double maxY = -Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < yData.size(); i++) {
			if (yData.get(i).compareTo(maxY) > 0) {
				maxY = yData.get(i);
			}
		}
		return maxY;
	}

	@Override
	public Double getMinY() {
		Double minY = Double.MAX_VALUE;
		for (int i = MinX; i <= maxX && i < yData.size(); i++) {
			if (yData.get(i).compareTo(minY) < 0) {
				minY = yData.get(i);
			}
		}
		return minY;
	}

	public Polyline(double[] yy, Chart chart, int index) {

		this(chart, index);
		int i = 1;
		for (double y : yy) {
			this.xData.add(new Double(i));
			yData.add(y);
			i++;
		}

	}

	public Polyline(List<Double> yData, Chart chart, int index) {

		this(chart, index);
		this.yData = yData;
		for (int i = 1; i <= yData.size(); i++) {
			this.xData.add(new Double(i));
		}

	}

	public Polyline(List daydealList, Chart chart, String propName, int index) {
		this(chart, index);
		int i = 1;
		for (Object object : daydealList) {
			this.xData.add(new Double(i));
			yData.add((Double) ReflectUtil.getFieldValue(object, propName));
			i++;
		}

	}

	public void addData(double x, double y) {
		xData.add(x);
		yData.add(y);

	}

	public void addData(double x, double y, String str) {
		this.addData(x, y);
		strData.add(str);

	}

	public void setUi(Color lineColor, int ovalRadius) {
		this.lineColor = lineColor;
		this.ovalRadius = ovalRadius;

	}

	public Polyline setUi(Color lineColor, int ovalRadius, boolean showLine) {
		setUi(lineColor, ovalRadius);
		this.showLine = showLine;
		return this;

	}

	/*
	 * 画折线图
	 */
	@Override
	public void draw(Graphics g) {
		int leng = maxX - MinX + 1;
		if (maxX > yData.size()-1) {
			leng = yData.size() - MinX;
		}
		g.setColor(lineColor);
		int[] xPixels = new int[leng];// x轴的像素值
		int[] yPixels = new int[leng];// y轴的像素值
		int pre = 0;
		int ii = 0;
		for (int i = MinX; i <= maxX && i < yData.size(); i++) {
			xPixels[ii] = chart.xAxis.getXAxisModel().translateValueToPx(xData.get(i));
			yPixels[ii] = chart.getYAxis(index).getYAxisModel().translateValueToPx(yData.get(i));
			// 设置点的颜色
			if (yPixels[ii] > pre) {
				g.setColor(Color.GREEN);
			} else {
				g.setColor(Color.RED);
			}
			pre = yPixels[ii];
			g.fillOval(xPixels[ii] - ovalRadius, yPixels[ii] - ovalRadius, ovalRadius * 2, ovalRadius * 2);
			g.setColor(Color.WHITE);
			if (strData.size() > 0) {
				g.drawString(strData.get(i), xPixels[ii], yPixels[ii]);
			}
			ii++;

		}
		g.setColor(lineColor);
		if (showLine) {
			g.drawPolyline(xPixels, yPixels, xPixels.length);

		}

	}

	/**
	 * @return Color
	 */
	public Color getLineColor() {
		return lineColor;
	}

	/**
	 * Sets the lineColor.
	 * 
	 * @param lineColor
	 *            The lineColor to set
	 */
	public void setLineColor(Color lineColor) {
		this.lineColor = lineColor;
	}

}